package com.baizhi.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baizhi.model.entity.User;
import com.baizhi.user.dao.UserMapper;
import com.baizhi.user.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author gange
 * @since 2023-07-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public Map<String, Object> selectUser(int page, int per_page, Integer id, String name, String phone) {
        Page p = new Page(page,per_page);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(id)){
            queryWrapper.eq("id",id);
        }
        if(ObjectUtil.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(ObjectUtil.isNotEmpty(phone)){
            queryWrapper.like("phone",phone);
        }
        IPage pageResult = page(p, queryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total_count",pageResult.getTotal());
        resultMap.put("items",pageResult.getRecords());
        return resultMap;
    }
}
